package com.geekhole.demo.user.application.v2.service;

import com.geekhole.demo.user.domain.CreateAndEnableUserContext;
import com.geekhole.demo.user.domain.User;
import com.geekhole.demo.user.application.v2.service.base.AbstractCreateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Service;

@Service
public class CreateAndEnableUserService
        extends AbstractCreateService<User, CreateAndEnableUserContext> {

    @Autowired
    public CreateAndEnableUserService(ApplicationEventPublisher eventPublisher, CrudRepository<User, ?> repository) {
        super(repository, eventPublisher);
    }

    @Override
    protected User instance(CreateAndEnableUserContext context) {
        // 实例化 User 聚合对象
        return User.create(context.getName(), context.getAge());
    }

    @Override
    protected void update(User agg, CreateAndEnableUserContext context) {
        // 调用聚合上的 业务方法
        agg.enable();
    }
}
